package edu.ncsu.csc.itrust.http;

import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebResponse;

/**
 * Black Box tests for HW4
 * @author pjfinega
 *
 */
public class PatientSpecificInstructionsTest extends iTrustHTTPTest {
	
	/**
     * Clear and setup database for independent testing
     */
    protected void setUp() throws Exception{
        super.setUp();
        gen.clearAllTables();
        gen.standardData();
        gen.hw4();
    }
    
    /**
     * First Acceptance Scenario
     */
    public void testAddInstructionToNewOfficeVisit() throws Exception{
    	//Get to page
    	WebConversation wc = login("9000000008", "pw");
        WebResponse wr = wc.getCurrentPage();
        wr = wr.getLinkWith("Document Office Visit").click();
 		WebForm patientForm = wr.getForms()[0];
 		patientForm.getScriptableObject().setParameterValue("UID_PATIENTID", "1");
 		patientForm.getButtons()[1].click();
 		wr = wc.getCurrentPage();
 		assertEquals(ADDRESS + "auth/hcp-uap/documentOfficeVisit.jsp", wr.getURL().toString());
 		WebForm form = wr.getForms()[0];
 		wr = wr.getLinkWith("01/28/2011").click();
		assertEquals("iTrust - Document Office Visit", wr.getTitle());
		
		//Add Information
		form = wr.getForms()[0];
		form.setParameter("addInstName", "Flu Diet");
		form.setParameter("addInstUrl", "http://www.webmd.com/cold-and-flu/flu-guide/what-to-eat-when-you-have-the-flu");
		form.setParameter("addInstComment", "Eat a healthy diet to help you get over the flu faster! Take your vitams and drink lots of fluids.");
		form.getButtonWithID("addinstruction").click();
		
		//Verify Added and Check page loads
		wr = wc.getCurrentPage();
		assertTrue(wr.getText().contains("Information Successfully Updated"));
		assertTrue(wr.getText().contains("http://www.webmd.com/cold-and-flu/flu-guide/what-to-eat-when-you-have"));
    }
    
    /**
     * Second Acceptance Scenario
     * @throws Exception 
     */
    public void testDeleteSpecificInstruction() throws Exception {
    	
    	setUp();
    	
    	//Get to page
    	WebConversation wc = login("9000000008", "pw");
        WebResponse wr = wc.getCurrentPage();
        wr = wr.getLinkWith("Document Office Visit").click();
 		WebForm patientForm = wr.getForms()[0];
 		patientForm.getScriptableObject().setParameterValue("UID_PATIENTID", "1");
 		patientForm.getButtons()[1].click();
 		wr = wc.getCurrentPage();
 		assertEquals(ADDRESS + "auth/hcp-uap/documentOfficeVisit.jsp", wr.getURL().toString());
 		WebForm form = wr.getForms()[0];
 		wr = wr.getLinkWith("01/28/2011").click();
		assertEquals("iTrust - Document Office Visit", wr.getTitle());
		
		//Add Information
		form = wr.getForms()[0];
		form.setParameter("addInstName", "Flu Diet");
		form.setParameter("addInstUrl", "http://www.webmd.com/cold-and-flu/flu-guide/what-to-eat-when-you-have-the-flu");
		form.setParameter("addInstComment", "Eat a healthy diet to help you get over the flu faster! Take your vitams and drink lots of fluids.");
		form.getButtonWithID("addinstruction").click();
		
		//Verify Added and Check page loads
		wr = wc.getCurrentPage();
		assertTrue(wr.getText().contains("Information Successfully Updated"));
		assertTrue(wr.getText().contains("http://www.webmd.com/cold-and-flu/flu-guide/what-to-eat-when-you-have"));
    
		//Remove instruction
		wr = wr.getLinkWith("Remove").click();
		
		//Verify changed
		wr = wc.getCurrentPage();
		assertEquals("iTrust - Document Office Visit", wr.getTitle());
		assertTrue(wr.getText().contains("Information Successfully Updated"));	
		assertFalse(wr.getText().contains("Remove"));
    }
    
    /**
     * Third Acceptance Scenario
     * @throws Exception
     */
    public void testAddInstructionToExisitingOfficeVisit() throws Exception {
    	
    	setUp();
    	
    	//Get to page
    	WebConversation wc = login("9000000003", "pw");
        WebResponse wr = wc.getCurrentPage();
        wr = wr.getLinkWith("Document Office Visit").click();
 		WebForm patientForm = wr.getForms()[0];
 		patientForm.getScriptableObject().setParameterValue("UID_PATIENTID", "5");
 		patientForm.getButtons()[1].click();
 		wr = wc.getCurrentPage();
 		assertEquals(ADDRESS + "auth/hcp-uap/documentOfficeVisit.jsp", wr.getURL().toString());
 		WebForm form = wr.getForms()[0];
 		wr = wr.getLinkWith("01/28/2011").click();
		assertEquals("iTrust - Document Office Visit", wr.getTitle());
    	
		//Add Information
		form = wr.getForms()[0];
		form.setParameter("addInstName", "Infant Milestones - 1 to 6 months");
		form.setParameter("addInstUrl", "http://www.babycenter.com/0_milestone-chart-1-to-6-months_1496585.bc");
		form.setParameter("addInstComment", "Watch for Baby Programmer rolling over soon. Make sure to prevent falls from furniture..");
		form.getButtonWithID("addinstruction").click();
		
		//Check info is added
		wr = wc.getCurrentPage();
		assertTrue(wr.getText().contains("Information Successfully Updated"));
		assertTrue(wr.getText().contains("Infant Milestones - 1 to 6 months"));
    }
    
    /**
     * Fourth Acceptance Test
     * @throws Exception
     */
    public void testAddInstructionPunctuation() throws Exception {
    	
    	setUp();
    	
    	//Get to page
    	WebConversation wc = login("9000000008", "pw");
        WebResponse wr = wc.getCurrentPage();
        wr = wr.getLinkWith("Document Office Visit").click();
 		WebForm patientForm = wr.getForms()[0];
 		patientForm.getScriptableObject().setParameterValue("UID_PATIENTID", "2");
 		patientForm.getButtons()[1].click();
 		wr = wc.getCurrentPage();
 		assertEquals(ADDRESS + "auth/hcp-uap/documentOfficeVisit.jsp", wr.getURL().toString());
 		WebForm form = wr.getForms()[0];
		form.getButtons()[0].click();
		wr = wc.getCurrentPage();
		assertEquals("iTrust - Document Office Visit", wr.getTitle());
    	
		//Add Information
		form = wr.getForms()[0];
		form.setParameter("addInstName", "Carpal Tunnel Syndrome Exercises");
		form.setParameter("addInstUrl", "http://www.eatonhand.com/hw/ctexercise.htm");
		form.setParameter("addInstComment", "Remember to try all the exercises. Use the following order: #1, #2 and #6; #3 - #5 are 'optional'. If you have any questions, please let me know!");
		form.getButtonWithID("addinstruction").click();
		
		//Check info is added
		wr = wc.getCurrentPage();
		assertTrue(wr.getText().contains("Information Successfully Updated"));
		assertTrue(wr.getText().contains("Carpal Tunnel Syndrome Exercises"));
    }
    
    /**
     * Fifth Acceptance Scenario
     */
    public void testPatientViewInstructions() throws Exception {
    	
    	setUp();
    	
    	//Get to page
    	WebConversation wc = login("2", "pw");
    	WebResponse wr = wc.getCurrentPage();
        wr = wr.getLinkWith("My Patient Specific Instructions").click();
        assertEquals("iTrust - My Patient Specific Instructions", wr.getTitle());
        
        //Check correct information
        assertTrue(wr.getText().contains("Heartburn Information"));
        assertTrue(wr.getText().contains("Vein Procedure Resource"));
        assertFalse(wr.getText().contains("Glucose Testing Information")); 		
    }
    
    /**
     * First Free Form Black Box Test
     * @throws Exception
     */
    public void testAddInstructionPunctuation2() throws Exception {
    	
    	setUp();
    	
    	//Get to page
    	WebConversation wc = login("9000000008", "pw");
        WebResponse wr = wc.getCurrentPage();
        wr = wr.getLinkWith("Document Office Visit").click();
 		WebForm patientForm = wr.getForms()[0];
 		patientForm.getScriptableObject().setParameterValue("UID_PATIENTID", "2");
 		patientForm.getButtons()[1].click();
 		wr = wc.getCurrentPage();
 		assertEquals(ADDRESS + "auth/hcp-uap/documentOfficeVisit.jsp", wr.getURL().toString());
 		WebForm form = wr.getForms()[0];
		form.getButtons()[0].click();
		wr = wc.getCurrentPage();
		assertEquals("iTrust - Document Office Visit", wr.getTitle());
    	
		//Add Information
		form = wr.getForms()[0];
		form.setParameter("addInstName", "Carpal Tunnel Syndrome Exercises");
		form.setParameter("addInstUrl", "http://www.eatonhand.com/hw/ctexercise.htm");
		form.setParameter("addInstComment", ";?/ ");
		form.getButtonWithID("addinstruction").click();
		
		//Check info is added
		wr = wc.getCurrentPage();
		assertTrue(wr.getText().contains("Information Successfully Updated"));
		assertTrue(wr.getText().contains("Carpal Tunnel Syndrome Exercises"));
    }
    
    /**
     * Second Free Form Black Box test
     * @throws Exception
     */
    public void testMaxNameField() throws Exception {
    	
    	setUp();
    	
    	//Get to page
    	WebConversation wc = login("9000000008", "pw");
        WebResponse wr = wc.getCurrentPage();
        wr = wr.getLinkWith("Document Office Visit").click();
 		WebForm patientForm = wr.getForms()[0];
 		patientForm.getScriptableObject().setParameterValue("UID_PATIENTID", "2");
 		patientForm.getButtons()[1].click();
 		wr = wc.getCurrentPage();
 		assertEquals(ADDRESS + "auth/hcp-uap/documentOfficeVisit.jsp", wr.getURL().toString());
 		WebForm form = wr.getForms()[0];
		form.getButtons()[0].click();
		wr = wc.getCurrentPage();
		assertEquals("iTrust - Document Office Visit", wr.getTitle());
    	
		//Add Information
		form = wr.getForms()[0];
		form.setParameter("addInstName", "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
		form.setParameter("addInstUrl", "http://www.eatonhand.com/hw/ctexercise.htm");
		form.setParameter("addInstComment", "Doesn't matter.");
		form.getButtonWithID("addinstruction").click();
		
		//Check info is added
		wr = wc.getCurrentPage();
		assertFalse(wr.getText().contains("Information Successfully Updated"));
		assertFalse(wr.getText().contains("Carpal Tunnel Syndrome Exercises"));
    }
    
    /**
     * Third Free Form Black Box Test
     * @throws Exception
     */
    public void testMaxURLField() throws Exception {
    	
    	setUp();
    	
    	//Get to page
    	WebConversation wc = login("9000000008", "pw");
        WebResponse wr = wc.getCurrentPage();
        wr = wr.getLinkWith("Document Office Visit").click();
 		WebForm patientForm = wr.getForms()[0];
 		patientForm.getScriptableObject().setParameterValue("UID_PATIENTID", "2");
 		patientForm.getButtons()[1].click();
 		wr = wc.getCurrentPage();
 		assertEquals(ADDRESS + "auth/hcp-uap/documentOfficeVisit.jsp", wr.getURL().toString());
 		WebForm form = wr.getForms()[0];
		form.getButtons()[0].click();
		wr = wc.getCurrentPage();
		assertEquals("iTrust - Document Office Visit", wr.getTitle());
    	
		//Add Information
		form = wr.getForms()[0];
		form.setParameter("addInstName", "Doesn't matter");
		form.setParameter("addInstUrl", "http://th.wikipedia.org/wiki/%E0%B9%82%E0%B8%A3%E0%B8%87%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B9%80%E0%B8%9A%E0%B8%8D%E0%B8%88%E0%B8%A1%E0%B8%A3%E0%B8%B2%E0%B8%8A%E0%B8%B9%E0%B8%97%E0%B8%B4%E0%B8%A8_%E0%B8%99%E0%B8%84%E0%B8%A3%E0%B8%A8%E0%B8%A3%E0%B8%B5%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%A3%E0%B8%B2%E0%B8%8A");
		form.setParameter("addInstComment", "Doesn't matter.");
		form.getButtonWithID("addinstruction").click();
		
		//Check info is added
		wr = wc.getCurrentPage();
		assertFalse(wr.getText().contains("Information Successfully Updated"));
		assertFalse(wr.getText().contains("Doesn't matter"));
    }
    
    /**
     * Fourth Free Form Black Box Test
     * @throws Exception
     */
    public void testMaxCommentField() throws Exception {
    	setUp();
    	
    	//Get to page
    	WebConversation wc = login("9000000008", "pw");
        WebResponse wr = wc.getCurrentPage();
        wr = wr.getLinkWith("Document Office Visit").click();
 		WebForm patientForm = wr.getForms()[0];
 		patientForm.getScriptableObject().setParameterValue("UID_PATIENTID", "2");
 		patientForm.getButtons()[1].click();
 		wr = wc.getCurrentPage();
 		assertEquals(ADDRESS + "auth/hcp-uap/documentOfficeVisit.jsp", wr.getURL().toString());
 		WebForm form = wr.getForms()[0];
		form.getButtons()[0].click();
		wr = wc.getCurrentPage();
		assertEquals("iTrust - Document Office Visit", wr.getTitle());
    	
		//Add Information
		form = wr.getForms()[0];
		form.setParameter("addInstName", "Doesn't matter");
		form.setParameter("addInstUrl", "http://www.eatonhand.com/hw/ctexercise.htm");
		form.setParameter("addInstComment", "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
		form.getButtonWithID("addinstruction").click();
		
		//Check info is added
		wr = wc.getCurrentPage();
		assertFalse(wr.getText().contains("Information Successfully Updated"));
		assertFalse(wr.getText().contains("Doesn't matter"));
    }
    
    /**
     * Fifth Free Form Black Box Test
     * @throws Exception
     */
    public void testNoInput() throws Exception {
    	setUp();
    	
    	//Get to page
    	WebConversation wc = login("9000000008", "pw");
        WebResponse wr = wc.getCurrentPage();
        wr = wr.getLinkWith("Document Office Visit").click();
 		WebForm patientForm = wr.getForms()[0];
 		patientForm.getScriptableObject().setParameterValue("UID_PATIENTID", "2");
 		patientForm.getButtons()[1].click();
 		wr = wc.getCurrentPage();
 		assertEquals(ADDRESS + "auth/hcp-uap/documentOfficeVisit.jsp", wr.getURL().toString());
 		WebForm form = wr.getForms()[0];
		form.getButtons()[0].click();
		wr = wc.getCurrentPage();
		assertEquals("iTrust - Document Office Visit", wr.getTitle());
    	
		//Add Information
		form = wr.getForms()[0];
		form.setParameter("addInstName", "");
		form.setParameter("addInstUrl", "");
		form.setParameter("addInstComment", "");
		form.getButtonWithID("addinstruction").click();
		
		//Check info is added
		wr = wc.getCurrentPage();
		assertFalse(wr.getText().contains("Information Successfully Updated"));
    }
}
